//
// Created by 抑~风 on 2022/10/16.
//
#ifndef UTIL
#define UTIL

#include<iostream>
#include<cstring>
#include<algorithm>
#include <chrono>

using std::ostream;

template <typename T>
struct TimeData {
    double count;
    T data;
    friend ostream &operator<<(ostream &os, const TimeData &res) {
        os << "res: " << res.data << " time: " << res.count << "ms" ;
        return os;
    }
};
template<typename T,typename A,typename... Args>
auto calcTime(T func,TimeData<A>data,Args... rest) -> decltype(data){
    auto start = std::chrono::system_clock::now();
    data.data = func(rest...);
    auto end = std::chrono::system_clock::now();
    std::chrono::duration<double> diff = end - start;
    data.count = diff.count();
    return data;
}



template<typename T,typename... Args>
auto calcTime(T&& func, Args&&... rest) -> double{
    auto start = std::chrono::system_clock::now();
    func(std::forward<Args>(rest)...);
    auto end = std::chrono::system_clock::now();
//    std::chrono::duration<double> diff = end-start;
    // 计算毫秒时间差并输出
    // 如果要求其他时间单位可以修改 std::chrono::milliseconds 为其他类型
    // 比如std::chrono::seconds
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
    return duration.count();
}


#endif
